package com.bawei.day12_broadcastrecevier;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.Toast;

public class Main6Activity extends AppCompatActivity {
    private MyMusicRecevicer myMusicRecevicer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main6);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            requestPermissions(new String[]{
                    "android.permission.WRITE_EXTERNAL_STORAGE",
                    "android.permission.READ_EXTERNAL_STORAGE"
            },101);
        }
        findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                notification();
            }
        });

        myMusicRecevicer = new MyMusicRecevicer();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.bawei.pre");
        intentFilter.addAction("com.bawei.play");
        registerReceiver(myMusicRecevicer,intentFilter);


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myMusicRecevicer);
    }

    //发送通知：自定义通知
    private void notification() {
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.drawable.icon);//必须设置
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification);
        //图片:sd卡
        remoteViews.setImageViewBitmap(R.id.iv, BitmapFactory.decodeFile("/sdcard/Pictures/hahah.jpg"));
        //文字
        remoteViews.setTextViewText(R.id.tv,"青花瓷");
        //点击监听结合广播：发送方
        Intent intent = new Intent();
        intent.setAction("com.bawei.pre");
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this,101,intent,PendingIntent.FLAG_ONE_SHOT);
        remoteViews.setOnClickPendingIntent(R.id.iv_pre,pendingIntent);


        Intent intent2 = new Intent();
        intent2.setAction("com.bawei.play");
        PendingIntent pendingIntent2 = PendingIntent.getBroadcast(this,101,intent2,PendingIntent.FLAG_ONE_SHOT);
        remoteViews.setOnClickPendingIntent(R.id.iv_play,pendingIntent2);

        builder.setCustomContentView(remoteViews);//设置自定义布局
        notificationManager.notify(1,builder.build());




    }

    class MyMusicRecevicer extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            if("com.bawei.pre".equals(intent.getAction())){
                Toast.makeText(context, "上一首", Toast.LENGTH_SHORT).show();
            }else if("com.bawei.play".equals(intent.getAction())){
                Toast.makeText(context, "播放", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
